package com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class MyXMLReader {

	public static void main(String[] args) {
		try {
			File f = new File("./WebRoot/xmlfiles/test1.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(f);
			Element root = doc.getRootElement();
			root.addElement("image").addElement("Name").addText("dragon");
			Element foo = root.element("image");
			foo.addElement("Name").addText("dragon");
			foo.addElement("URL").addText("pics/dragon.jpg");
			foo.addElement("Tags").addText("��|�й�|����");

			XMLWriter writer;      
		    	try {      
		            OutputFormat format = OutputFormat.createPrettyPrint();      
		            format.setEncoding("gb2312");      
		            FileOutputStream fos = new FileOutputStream("./WebRoot/xmlfiles/test2.xml");           
		            writer = new XMLWriter(fos, format);      
		            writer.write(doc);      
		            writer.close();      
		        } catch (IOException e) {      
		            // TODO �Զ���� catch ��           
		            e.printStackTrace();      
		        }   
/*			foo = root.element("image");
			for (Iterator i = root.elementIterator("image"); i.hasNext();) {
				foo = (Element) i.next();
				System.out.print("ͼƬ���:" + foo.elementText("Name"));
				System.out.print("  ͼƬ��ַ:" + foo.elementText("URL"));
				System.out.println("  ͼƬ��ǩ:" + foo.elementText("Tags"));
			}
		*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
}
